import random

def go(start,goone,end,time=0):
    for whogo in goone:
        start.remove(whogo)
        end.append(whogo)
    start.sort()
    end.sort()

    time+=max(goone)
    return time

  
def back(start,end,time=0):
    backone=min(end)
    start.append(backone)
    end.remove(backone)
    start.sort()
    
    time+=backone
    return time

def get_goone(start):
    goone=[]
    m=[]
    m[:]=start[:]
    if len(start)==1:
        goone.append(start)
    else:
        for i in start:
            m.remove(i)
            for j in m:
                goone.append([i,j])
    return goone
def random_goone(goonelist):
    return random.choice(goonelist)


time_list=[]
for k in range(500):
    start=[1,2,5,10]
    end=[]
    time=0
    m=len(start)
    trace=[]
    while len(end)<m:
        goone=random_goone(get_goone(start))
        time=go(start,goone,end,time)
        if start!=[]:
            time=back(start,end,time)
        trace.append(goone)
    print(trace,time)
    time_list.append(time)
print(time_list)
print(min(time_list))
    
